package com.litchi.tutor.common.util;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ReflectUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.extern.slf4j.Slf4j;

import java.util.List;
import java.util.stream.Collectors;

/**
 * @author hoby
 * @since 2021-08-19
 */
@Slf4j
public class PageUtil {

    public static <T> Page<T> transform(Page<?> page, Class<T> clazz) {
        Page<T> newPage = new Page<>();
        if (page == null) {
            return newPage;
        }
        BeanUtil.copyProperties(page, newPage, "records");
        List<T> records = page.getRecords().stream().map(item -> {
            T t = ReflectUtil.newInstance(clazz);
            BeanUtil.copyProperties(item, t);
            return t;
        }).collect(Collectors.toList());
        newPage.setRecords(records);
        return newPage;
    }
}
